package com.example.loginexample;

import com.example.db.DataBaseAdapter;

import android.support.v7.app.ActionBarActivity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class HomeActivity extends ActionBarActivity {

    Button btnSignIn, btnSignUp;
    DataBaseAdapter dbAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // create instance db
        dbAdapter = new DataBaseAdapter(this);
        dbAdapter = dbAdapter.open();

        // Get reference of button signin. signup
        btnSignIn = (Button) findViewById(R.id.btnSignin);
        btnSignUp = (Button) findViewById(R.id.btnSignup);

        btnSignUp.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intentSignUp = new Intent(getApplicationContext(),
                        SignUpActivity.class);
                startActivity(intentSignUp);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.hom, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /*
     * Method to handle signIn
     */
    public void signIn(View v) {
        final Dialog dialog = new Dialog(HomeActivity.this);
        dialog.setContentView(R.layout.login);
        dialog.setTitle("Login");

        // get reference of edittext
        final EditText etUsername = (EditText) dialog
                .findViewById(R.id.userName);
        final EditText etPassword = (EditText) dialog
                .findViewById(R.id.passWord);
        Button btnSignin = (Button) dialog.findViewById(R.id.btnSignin);

        // Set on clicklistener
        btnSignin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // get user name and password
                String userName = etUsername.getText().toString();
                String password = etPassword.getText().toString();

                if (userName.equals(null) || userName.equals("")
                        || password.equals(null) || password.equals("")) {
                    Toast.makeText(HomeActivity.this,
                            "Please enter your user name & password",
                            Toast.LENGTH_LONG).show();
                } else {
                    // check username and password in database
                    String _password = dbAdapter.getSingleEntry(userName);
                    if (password.equals(_password)) { //
                        Toast.makeText(HomeActivity.this, "Login successfull",
                                Toast.LENGTH_LONG).show();
                        dialog.dismiss();
                    } else {
                        Toast.makeText(HomeActivity.this,
                                "Login failed! Try again", Toast.LENGTH_LONG)
                                .show();
                        etUsername.setText("");
                        etPassword.setText("");
                    }

                }

            }
        });

        dialog.show();
    }
}
